new Vue({
    el: "#container",
    data() {
        return {
            //  生产方式
            tabIndentPro: 0,
            //  浇筑方式
            castModels: [],
            //  任务单浇筑方式
            tabIndentCast: -1,
            // 查询数据
            tableSearch: {
                dateFormat: "yyyy-MM-dd HH:mm",
                //  任务单生产状态
                proType: null,
            },
            //  显示的生产线数据
            proLines: [],
            //  排队车辆
            queueCars: [],
            //  生产完成的发货单
            completedInvs: [],
            //  运输车辆
            tranInvs: [],
            //  其他车辆
            otherCars: [],
            workIndentLoading: false,
            //  正供任务单
            allWorkIndents: [],
            workIndents: [],
            //  当前激活的任务单
            activeIndentCard: null,
            // 任务单的生产线列表
            indentLines: [],
            //  任务单泵送设备列表
            indentPumpDevs: [],
            pumpDeviceLoading: false,
            //  上传任务单弹窗
            uploadDialog: {
                show: false,
                loading: false,
                indent: {},
                totalStere: null,
            },
            //  上传工控机表单信息
            uploadFrom: {
                //  任务单
                indentId: null,
                //  发货类型
                invoiceType: '0',
                //  当发货类型为砂浆时,显示砂浆是否累计
                sandAccount: '0',
                //  生产线
                lineId: null,
                //  生产方量
                uploadStere: null,
                //  发货方量
                sendStere: null,
                //  泵送设备
                pumpDeviceId: null,
                //  车辆id
                carId: null,
                //  司机
                driver: null,
                //  司机id
                driverId: null,
                //  备注
                remark: null,
                //  累计车次
                totalCar: null,
                //  累计方量
                totalStere: null,
            },
            //  发货单详情
            invoiceDetail: {},
            //  待供任务单数据
            waitIndentDialog: {
                show: false,
                loading: false,
            },
            //  待供查询数据
            waitSearch: {
                dateFormat: "yyyy-MM-dd HH:mm",
                startDate: null,
                endDate: null,
                conName: "",
                site: "",
                //  任务单生产状态
                proType: 0,
            },
            //  待供任务单列表
            waitIndentList: [],
            waitIndentIds: [],
            //  生产线的车辆
            disPopover: false,
            // 是否需要弹单
            needPopUp: 0,
            //  弹单处理
            popUpDialog: {
                show: false,
                loading: false,
            },
            //  弹窗的发货单
            popUpInvoiceForm: {},
            //  全部车辆列表
            allCarList: [],
            //  道路类型
            roadTypeList: [],
            //  整车转料弹窗
            tranDialog: {
                show: false,
            },
            //  转料记录弹窗
            tranRecordDialog: {
                show: false,
                id: null,
            },
            //  更换车辆弹窗
            replaceCarDialog: {
                show: false,
                loading: false,
            },
            replaceCarForm: {
                //  发货单信息
                invoiceId: null,
                //  车辆信息
                carId: null,
                //  司机id
                driverId: null,
                //  司机名称
                driver: null,
            },
            //  弹单时候的发货单补料弹窗以及表单
            addInvDispatchDialog: {
                show: false,
                loading: false,
            },
            addInvDispatchForm: {
                //  发货单
                invoiceId: null,
                lineId: null,
                stere: null,
            },
            //  退砼记录
            invBackList: [],
            invBakPopover: false,
            //  退砼生产弹窗以及表单
            invBakDispatchDialog: {
                show: false,
                loading: false,
                totalStere: null,
            },
            invBakDispatchForm: {
                //  退砼单
                invBakId: null,
                //  任务单
                indentId: null,
                //  生产线
                lineId: null,
                //  生产方量
                uploadStere: null,
                //  发货方量
                sendStere: null,
                //  备注
                remark: null,
                //  累计车辆和车次
                totalCar: null,
                totalStere: null,
            },
            //  司机列表
            driverList: [],
            chooseCar: null,
            chooseDriver: null,
            //  表数据
            invoiceDatas: [],
            //  搜索车辆熟悉
            carSearch: {
                queue: null,
                other: null,
            },
            //  详情数据
            detailDialog: {
                show: false,
                invoiceId: false,
            },
            indentBoxPage: 0,
            //  近10车
            nearlyInvList: [],
            //  分车转料
            splitDialog: {
                show: false,
            },
        }
    },
    // 在模板渲染成html前调用
    created() {
        var that = this;
        that.loadWorkIndents();
        that.loadBaseDatas();
        that.refreshDatas();
    },
    // 在模板渲染成html后调用，通常是初始化页面完成后，再对html的dom节点进行一些需要的操作
    mounted() {
        var that = this;
    },
    methods: {
        //  刷新数据方法
        refreshDatas() {
            let that = this;
            //  加载生产线
            that.loadProLines();
            //  加载车辆
            that.loadCarList(0);
            //  加载发货单
            that.loadDisInvPage();
            //  退回操作
            that.loadInvBackList();
            //  加载任务单累计方量
            that.loadAccuAndTran();
            //  加载近10车
            that.loadNearlyInvList();
            setTimeout(function () {
                that.refreshDatas();
            }, 2000);
        },
        //  加载各个基础数据
        loadBaseDatas() {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/base/datas",
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        let map = res.data;
                        /*that.createTypeList = map.createTypeList;
                        that.invoiceTypeList = map.invoiceTypeList;
                        that.receiptTypeList = map.receiptTypeList;
                        that.specialClearTypeList = map.specialClearTypeList;*/
                        that.roadTypeList = map.roadTypeList;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询泵送设备失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.tableLoading = false;
                }
            })
        },
        //  加载生产线信息
        loadProLines() {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/dispatch/line/producing",
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        //  此处的数据是加载的派单数据
                        that.proLines = res.data;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询排队的车辆列表失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求排队的车辆列表失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        //  加载调度页面所需的车辆
        loadDispatchCars() {

        },
        //  加载任务单
        loadWorkIndents() {
            let that = this;
            // that.workIndentLoading = true;
            that.invoiceDatas = [];
            $.ajax({
                type: "POST",
                url: "/pro/indent/work/list",
                async: true,
                dataType: 'json',
                data: that.tableSearch,
                success: function (res) {
                    if (res.code == 0) {
                        let workIndents = res.data;
                        that.allWorkIndents = workIndents;
                        that.workIndents = workIndents;
                        //  根据任务单浇筑方式分组
                        let castS = [];
                        for (let i = 0, s = workIndents.length; i < s; i++) {
                            let indent = workIndents[i];
                            let castMn = indent.castModelName;
                            if (!(castS.indexOf(castMn) > -1)) {
                                //  没有数据
                                castS.push(castMn);
                            }
                        }
                        castS.sort((a, b) => b.localeCompare(a));
                        that.tabIndentCast = -1;
                        that.castModels = castS;
                        that.loadAccuAndTran();
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询任务单失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.workIndentLoading = false;
                }
            })
        },
        /**
         * 显示上传派单数据
         * @param val
         */
        showUploadDialog(index, indent) {
            let that = this;
            that.changeActiveIndent(index, indent);
            let indentId = indent.id;
            //  判断任务单是否暂供
            if (indent.type != 1) {
                that.$message({
                    showClose: true,
                    message: '该任务单不是正供状态,不允许派单操作',
                    type: 'error'
                });
                return;
            }
            //  加载任务单上传的生产线
            that.uploadDialog.loading = true;
            that.indentPumpDevs = null;
            that.loadIndentLines(indentId, function (indentLines) {
                that.uploadDialog.show = true;
                that.uploadDialog.indent = indent;
                that.indentLines = indentLines;
                //  设置表单数据
                that.uploadFrom = {
                    indentId: indentId,
                    invoiceType: '0',
                    sandAccount: '0',
                    lineId: null,
                    uploadStere: null,
                    sendStere: null,
                    pumpDeviceId: null,
                    carId: null,
                    driver: null,
                    remark: null,
                    //  累计车次
                    totalCar: null,
                    //  累计车辆
                    totalStere: null,
                }
                that.chooseCar = null;
                that.chooseDriver = null;
                //  查询任务单的泵送设备
                // that.loadIndentPumpDevs(indentId);
                //  查询任务单的累计车次和方量
                $.ajax({
                    type: "POST",
                    url: "/pro/indent/queryTotal",
                    async: true,
                    dataType: 'json',
                    data: {
                        indentId: indentId,
                    },
                    success: function (res) {
                        if (res.code == 0) {
                            let totalData = res.data;
                            let totalCar = totalData.totalCar;
                            //  累计车次默认+1
                            totalCar = parseInt(totalCar) + 1;
                            that.uploadFrom.totalCar = totalCar;
                            //  任务单的最大累计车次
                            let totalStere = totalData.totalStere;
                            that.uploadFrom.totalStere = totalStere;
                            that.uploadDialog.totalStere = totalStere;
                        } else {
                            that.$message({
                                showClose: true,
                                message: '查询任务单累计数据失败：' + res.msg,
                                type: 'error'
                            });
                        }
                    },
                    error: function (res) {
                        that.$message({
                            showClose: true,
                            message: '查询任务单累计数据失败',
                            type: 'error'
                        });
                        return;
                    },
                    complete: function (XMLHttpRequest, status) {
                    }
                })
            }, function () {
                that.uploadDialog.loading = false;
            });
        },
        //  加载任务单的生产线
        loadIndentLines(indentId, sucFunc, compFunc) {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/lab/indent/lines",
                async: true,
                dataType: 'json',
                data: {
                    indentId: indentId,
                },
                success: function (res) {
                    if (res.code == 0) {
                        if (sucFunc != null) {
                            sucFunc(res.data);
                        }
                    } else {
                        that.$message({
                            showClose: true,
                            message: res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    if (compFunc != null) {
                        compFunc();
                    }
                }
            });
        },
        /**
         * 加载泵送设备
         */
        loadIndentPumpDevs(indentId) {
            let that = this;
            that.pumpDeviceLoading = true;
            $.ajax({
                type: "POST",
                url: "/pro/indent/pump-dev-list",
                async: true,
                dataType: 'json',
                data: {
                    indentId: indentId,
                },
                success: function (res) {
                    if (res.code == 0) {
                        that.indentPumpDevs = res.data;
                        if (that.indentPumpDevs.length > 0) {
                            that.uploadFrom.pumpDeviceId = that.indentPumpDevs[0].id;
                        }
                    } else {
                        that.$message({
                            showClose: true,
                            message: "查询泵送设备失败" + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.pumpDeviceLoading = false;
                }
            });
        },

        /**
         * 上传到生产线
         */
        uploadLine() {
            let that = this;
            //  获取生产线
            let lineId = that.uploadFrom.lineId
            if (isNull(lineId)) {
                that.$message({
                    showClose: true,
                    message: '请选择生产线',
                    type: 'error'
                });
                return;
            }
            //  获取砼方量或者砂浆方量
            let uploadStere = that.uploadFrom.uploadStere;
            if ((isNull(uploadStere) || uploadStere <= 0)) {
                that.$message({
                    showClose: true,
                    message: '请输入生产方量',
                    type: 'error'
                });
                return;
            }
            let sendStere = that.uploadFrom.sendStere;
            if ((isNull(sendStere) || sendStere <= 0)) {
                that.$message({
                    showClose: true,
                    message: '请输入发货方量',
                    type: 'error'
                });
                return;
            }
            //  泵送设备
            /*let indentPumpDevs = that.indentPumpDevs;
            if (!isNull(indentPumpDevs) && indentPumpDevs.length > 0) {
                //  验证选择泵送设备
                let pumpDeviceId = that.uploadFrom.pumpDeviceId;
                if (pumpDeviceId == null) {
                    that.$message({
                        showClose: true,
                        message: '请选择泵送设备',
                        type: 'error'
                    });
                    return;
                }
            }*/
            //  验证车辆
            let carId = that.uploadFrom.carId;
            if (isNull(carId)) {
                that.$message({
                    showClose: true,
                    message: '请选择运输车辆',
                    type: 'error'
                });
                return;
            }
            //  上传数据
            that.uploadDialog.loading = true;
            $.ajax({
                type: "PUT",
                url: "/pro/dispatch/do",
                async: true,
                dataType: 'json',
                data: that.uploadFrom,
                success: function (res) {
                    if (res.code == 0) {
                        that.$message({
                            showClose: true,
                            message: "派单成功,等待工控接收",
                            type: 'success'
                        });
                        //  启用定时任务,查询派单结果,一个定时器最多查询1分钟
                        let tNum = 0;
                        var timer = setInterval(function () {
                            //  查询派单消息
                            that.queryDisError();
                            tNum += 1;
                            if (tNum >= 30) {
                                clearInterval(timer);
                            }
                        }, 2000);
                        //  加载派单列表
                        that.loadProLines();
                        //  关闭弹窗
                        that.uploadDialog.show = false;
                    } else {
                        that.$message({
                            showClose: true,
                            message: "派单失败:" + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.uploadDialog.loading = false;
                }
            });
        },
        /**
         * 激活任务单
         */
        changeActiveIndent(index, indent) {
            let that = this;
            that.activeIndentCard = index;
        },
        /**
         * 加载发货单详细信息
         */
        loadInvDetail(inv, column, event) {
            let that = this;
            if (isNull(inv)) {
                return;
            }
            let invoiceId = inv.invoiceId;
            if (isNull(invoiceId)) {
                return;
            }
            that.detailDialog.show = true;
            that.detailDialog.invoiceId = invoiceId;
        },
        loadInvMiniDetail(invId) {
            let that = this;
            if (isNull(invId)) {
                return;
            }
            that.proLineCarLoading = true;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/overview/" + invId,
                async: true,
                dataType: 'json',
                success: function (res) {
                    if (res.code == 0) {
                        that.invoiceDetail = res.data;
                    } else {
                        that.$message({
                            showClose: true,
                            message: "查询发货单详情失败:" + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.proLineCarLoading = false;
                }
            });
        },
        /**
         * 更改任务单状态
         * @param indentId
         * @param proType
         */
        updateProType(indentId, proType) {
            let that = this;
            if (isNull(indentId)) {
                that.$message({
                    showClose: true,
                    message: '请选择正确的任务单',
                    type: 'error'
                });
                return;
            }
            let chooseDataIds = [indentId];
            chooseDataIds = JSON.stringify(chooseDataIds);
            $.ajax({
                type: "PUT",
                url: "/pro/indent/update/pro",
                async: true,
                dataType: 'json',
                data: {
                    indentIds: chooseDataIds,
                    proType: proType,
                },
                success: function (res) {
                    if (res.code == 0) {
                        that.loadWorkIndents();
                    } else {
                        that.$message({
                            showClose: true,
                            message: '更新任务单状态失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        /**
         * 列表的生产状态筛选
         * @param proType
         */
        intsTypeChange(proType) {
            let that = this;
            if (proType == 1) {
                that.tableSearch.proType = 1;
            } else if (proType == 3) {
                that.tableSearch.proType = 3;
            } else {
                that.tableSearch.proType = null;
            }
            that.loadWorkIndents();
        },
        /**
         * 待供任务单列表
         */
        showWaitIndent() {
            let that = this;
            that.waitIndentDialog.show = true;
            that.loadWaitIndents();
        },
        //  加载待供任务单列表
        loadWaitIndents() {
            let that = this;
            that.waitIndentDialog.loading = true;
            $.ajax({
                type: "POST",
                url: "/pro/indent/list",
                async: true,
                dataType: 'json',
                data: that.waitSearch,
                success: function (res) {
                    if (res.code == 0) {
                        that.waitIndentList = res.data;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询任务单失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.waitIndentDialog.loading = false;
                }
            })
        },
        //  选择了选择框
        waitIndentChange(val) {
            let that = this;
            that.waitIndentIds = val;
        },
        /**
         *  转正供任务单
         */
        tranProIndent() {
            let that = this;
            let waitIndentIds = that.waitIndentIds;
            if (waitIndentIds == null || waitIndentIds.length < 1) {
                that.$message({
                    showClose: true,
                    message: '请选择待供任务单',
                    type: 'error'
                });
                return;
            }
            //  只需要id
            let chooseDataIds = [];
            for (let i = 0, s = waitIndentIds.length; i < s; i++) {
                let cd = waitIndentIds[i];
                chooseDataIds.push(cd.id);
            }
            chooseDataIds = JSON.stringify(chooseDataIds);
            that.waitIndentDialog.loading = true;
            $.ajax({
                type: "PUT",
                url: "/pro/indent/update/pro",
                async: true,
                dataType: 'json',
                data: {
                    indentIds: chooseDataIds,
                    proType: 1,
                },
                success: function (res) {
                    if (res.code == 0) {
                        that.loadWaitIndents();
                        that.loadWorkIndents();
                    } else {
                        that.$message({
                            showClose: true,
                            message: '更新任务单状态失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.waitIndentDialog.loading = false;
                }
            })
        },
        /**
         * 删除派单
         * @param disId
         */
        delDispatch(disId) {
            let that = this;
            $.ajax({
                type: "DELETE",
                url: "/pro/dispatch/del/" + disId,
                async: true,
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        //  重新加载数据
                        that.$message({
                            showClose: true,
                            message: '发送指令成功,请等待操作结果',
                            type: 'success'
                        });
                        // that.loadProLines();
                        that.disPopover = false;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '删除派单失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '删除派单失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.waitIndentDialog.loading = false;
                }
            })
        },
        /**
         * 补料
         * @param invoiceId     发货单id
         * @param disId         派单id
         */
        addDispatch(invoiceId, disId) {
            let that = this;
            if (isNull(invoiceId) || isNull(disId)) {
                that.$message({
                    showClose: true,
                    message: "请选择待补料的派车信息",
                    type: 'error'
                });
                return;
            }
            that.$prompt('请输入补料方量', '补料提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^(?:0\.[1-9]\d*|[1-9]\d*(\.\d+)?)$/,
                inputErrorMessage: '请输入正确的方量',
                center: true
            }).then(({value}) => {
                $.ajax({
                    type: "PUT",
                    url: "/pro/dispatch/add/stere",
                    async: true,
                    data: {
                        disId: disId,
                        invoiceId: invoiceId,
                        stere: value,
                    },
                    success: function (res) {
                        if (res.code == 0) {
                            //  重新加载数据
                            that.$message({
                                showClose: true,
                                message: '发送指令成功,请等待操作结果',
                                type: 'success'
                            });
                            // that.loadProLines();
                            that.disPopover = false;
                        } else {
                            that.$message({
                                showClose: true,
                                message: '上传补料失败：' + res.msg,
                                type: 'error'
                            });
                        }
                    },
                    error: function (res) {
                        that.$message({
                            showClose: true,
                            message: '上传补料信息失败，请联系管理员',
                            type: 'error'
                        });
                        return;
                    },
                    complete: function (XMLHttpRequest, status) {
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消操作'
                });
            })
        },
        /**
         * 打卡
         * @param carId
         */
        carClock(carId, carCode, carStatus) {
            let that = this;
            $.ajax({
                type: "PUT",
                url: "/pro/tran/car/clock/" + carId,
                async: true,
                data: {
                    carStatus: carStatus,
                },
                success: function (res) {
                    if (res.code == 0) {
                        //  重新加载数据
                        that.$message({
                            showClose: true,
                            message: carCode + "操作成功",
                            type: 'success'
                        });
                        that.loadCarList(1);
                    } else {
                        that.$message({
                            showClose: true,
                            message: '操作失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '上号异常，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        //  加载车辆
        loadCarList(re) {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/tran/car/list",
                async: true,
                dataType: 'json',
                data: {
                    re: re
                },
                success: function (res) {
                    if (res.code == 0) {
                        let allCarList = res.data;
                        that.allCarList = allCarList;
                        //  分组
                        let queueCars = [];
                        let otherCars = [];
                        let completedInvs = [];
                        let tranInvs = [];
                        for (let i = 0, s = allCarList.length; i < s; i++) {
                            let car = allCarList[i];
                            if (car.carStatus == 1) {
                                //  排队中
                                let qs = that.carSearch.queue;
                                if (!isNull(qs)) {
                                    //  只匹配筛选数据
                                    if (!isNull(car.code) && car.code.indexOf(qs) > -1) {
                                        queueCars.push(car);
                                    } else if (!isNull(car.carNo) && car.carNo.indexOf(qs) > -1) {
                                        queueCars.push(car);
                                    }
                                } else {
                                    queueCars.push(car);
                                }
                            } else if (car.carStatus == 3) {
                                //  已生产
                                completedInvs.push(car);
                            } else if (car.carStatus == 4) {
                                //  运输中
                                tranInvs.push(car);
                            } else {
                                //  不在排队也不在生产中
                                let qs = that.carSearch.other;
                                if (!isNull(qs)) {
                                    //  只匹配筛选数据
                                    if (!isNull(car.code) && car.code.indexOf(qs) > -1) {
                                        otherCars.push(car);
                                    } else if (!isNull(car.carNo) && car.carNo.indexOf(qs) > -1) {
                                        otherCars.push(car);
                                    }
                                } else {
                                    otherCars.push(car);
                                }
                            }
                        }
                        that.queueCars = queueCars;
                        that.otherCars = otherCars;
                        that.completedInvs = completedInvs;
                        that.tranInvs = tranInvs;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询车辆列表失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求车辆列表失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        //  修改车辆弹窗
        showReplaceCar(invoiceId) {
            let that = this;
            if (isNull(invoiceId)) {
                that.$message({
                    showClose: true,
                    message: '请选择发货单',
                    type: 'error'
                });
                return;
            }
            that.replaceCarForm.invoiceId = invoiceId;
            that.replaceCarDialog.show = true;
        },
        //  提交修改车辆弹窗
        submitReplaceCar() {
            let that = this;
            if (isNull(that.replaceCarForm.carId)) {
                that.$message({
                    showClose: true,
                    message: '请选择车辆',
                    type: 'error'
                });
                return;
            }
            if (isNull(that.replaceCarForm.driver)) {
                that.$message({
                    showClose: true,
                    message: '请输入司机',
                    type: 'error'
                });
                return;
            }
            that.replaceCarDialog.loading = true;
            //  上传数据
            $.ajax({
                type: "PUT",
                url: "/pro/dispatch/replace/car",
                async: true,
                dataType: 'json',
                data: {
                    invoiceId: that.replaceCarForm.invoiceId,
                    driverId: that.replaceCarForm.driverId,
                    driver: that.replaceCarForm.driver,
                    carId: that.replaceCarForm.carId,
                },
                success: function (res) {
                    if (res.code == 0) {
                        // that.allCarList = res.data;
                        that.$message({
                            showClose: true,
                            message: '操作成功',
                            type: 'success'
                        });
                    } else {
                        that.$message({
                            showClose: true,
                            message: '操作失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '操作失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.replaceCarDialog.loading = false;
                }
            })
        },
        //  取消换车弹窗
        cancelReplaceCar() {
            let that = this;
            that.replaceCarForm.invoiceId = null;
            that.replaceCarForm.carId = null;
            that.replaceCarForm.driver = null;
            that.replaceCarDialog.show = false;
        },
        /**
         * 显示弹窗
         */
        showPopUp() {
            let that = this;
            let needPopUp = that.needPopUp;
            if (needPopUp == 1) {
                //  开启弹单
                if (that.popUpDialog.show == false) {
                    //  查询最新数据
                    $.ajax({
                        type: "POST",
                        url: "/pro/dispatch/pup-up",
                        async: true,
                        data: {},
                        success: function (res) {
                            if (res.code == 0) {
                                that.popUpInvoiceForm = res.data;
                                that.popUpDialog.show = true;
                            } else {
                            }
                        },
                        error: function (res) {
                            return;
                        },
                        complete: function (XMLHttpRequest, status) {
                            setTimeout(function () {
                                that.showPopUp();
                            }, 2000)
                        }
                    })
                } else {
                    setTimeout(function () {
                        that.showPopUp();
                    }, 2000)
                }
            } else {
                // 关闭弹单
            }
        },
        /**
         * 保存弹单数据
         */
        savePopInvoice() {
            let that = this;
            let invoiceId = that.popUpInvoiceForm.id;
            that.popUpDialog.loading = true;
            // that.popUpInvoiceForm.isAdd = 1;
            $.ajax({
                type: "PUT",
                url: "/pro/invoice/update/" + invoiceId,
                async: true,
                data: that.popUpInvoiceForm,
                success: function (res) {
                    if (res.code == 0) {
                        /*that.$message({
                            showClose: true,
                            message: '保存成功',
                            type: 'success'
                        });*/
                        that.$confirm('保存成功,是否打印?', '温馨提示', {
                            confirmButtonText: '打印',
                            cancelButtonText: '取消',
                            type: 'success'
                        }).then(() => {
                            that.printPopInvoice();
                        }).catch(() => {
                            // that.popUpDialog.show = false;
                        });
                    } else {
                        that.$message({
                            showClose: true,
                            message: '保存失败:' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.popUpDialog.loading = false;
                }
            })
        },
        /**
         * 处理弹单的打印
         */
        printPopInvoice() {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/dispatch/print/" + that.popUpInvoiceForm.id,
                async: true,
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        that.$message({
                            showClose: true,
                            message: '开始打印',
                            type: 'success'
                        });
                    } else {
                        that.$message({
                            showClose: true,
                            message: '打印失败:' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        //  作废发货单
        scrapInvoice(invoiceId) {
            let that = this;
            this.$prompt('', '请输入作废原因', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                center: true
            }).then(({value}) => {
                that.tableLoading = true;
                $.ajax({
                    type: "DELETE",
                    url: "/pro/invoice/scrap/" + invoiceId,
                    async: true,
                    dataType: 'json',
                    data: {
                        remark: value,
                    },
                    success: function (res) {
                        if (res.code == 0) {
                            that.$message({
                                showClose: true,
                                message: '作废成功',
                                type: 'success'
                            });
                            // that.tranDialog.show = false;
                        } else {
                            that.$message({
                                showClose: true,
                                message: '作废失败：' + res.msg,
                                type: 'error'
                            });
                        }
                    },
                    error: function (res) {
                        that.$message({
                            showClose: true,
                            message: '请求失败，请联系管理员',
                            type: 'error'
                        });
                        return;
                    },
                    complete: function (XMLHttpRequest, status) {
                        that.tableLoading = false;
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消作废操作'
                });
            });
        },
        //  显示发货单退剩转记录
        showInvHandle() {
            let that = this;
            that.tranRecordDialog.id = that.popUpInvoiceForm.id;
            that.tranRecordDialog.show = true;
        },
        /**
         *  发货单生产后的补料
         */
        showInvDispatch(invId, indentId) {
            let that = this;
            that.addInvDispatchForm.invoiceId = invId;
            that.addInvDispatchDialog.show = true;
            //  加载生产线
            that.addInvDispatchDialog.loading = true;
            that.loadIndentLines(indentId, function (indentLines) {
                //  生产线数据
                that.indentLines = indentLines;
            }, function () {
                that.addInvDispatchDialog.loading = false;
            });
        },
        //  提交表单
        submitInvDisForm() {
            let that = this;
            //  验证参数
            let invoiceId = that.addInvDispatchForm.invoiceId;
            if (isNull(invoiceId)) {
                that.$message({
                    showClose: true,
                    message: '请选择发货单',
                    type: 'error'
                });
                return;
            }
            let lineId = that.addInvDispatchForm.lineId;
            if (isNull(lineId)) {
                that.$message({
                    showClose: true,
                    message: '请选择生产线',
                    type: 'error'
                });
                return;
            }
            let stere = that.addInvDispatchForm.stere;
            if (isNull(stere) || !isNumber(stere) || stere < 0) {
                that.$message({
                    showClose: true,
                    message: '请输入派单方量',
                    type: 'error'
                });
                return;
            }
            that.addInvDispatchDialog.loading = true;
            $.ajax({
                type: "PUT",
                url: "/pro/dispatch/add/inv",
                async: true,
                dataType: 'json',
                data: {
                    invoiceId: invoiceId,
                    lineId: lineId,
                    stere: stere,
                },
                success: function (res) {
                    if (res.code == 0) {
                        //  生产线数据
                        that.cancelInvDisForm();
                        that.$message({
                            showClose: true,
                            message: "操作成功,请等待生产",
                            type: 'success'
                        });
                    } else {
                        that.$message({
                            showClose: true,
                            message: res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '操作失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.addInvDispatchDialog.loading = false;
                }
            });

        },
        //  取消弹窗
        cancelInvDisForm() {
            let that = this;
            that.addInvDispatchForm = {
                //  发货单
                invoiceId: null,
                lineId: null,
                stere: null,
            }
            that.addInvDispatchDialog.show = false;
        },
        //  查询退砼处理数据
        loadInvBackList() {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/back/nearly",
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        //  生产线数据
                        that.invBackList = res.data;
                    } else {
                        that.$message({
                            showClose: true,
                            message: "查询退砼处理失败:" + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '操作失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            });
        },
        //  退砼处理作废功能
        scrapInvBack(invBackId, stere) {
            let that = this;
            that.$prompt('请输入作废原因', '作废提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                center: true
            }).then(({value}) => {
                $.ajax({
                    type: "PUT",
                    url: "/pro/invoice/back/handle/" + invBackId,
                    async: true,
                    data: {
                        stere: stere,
                        type: 0,
                        remark: value
                    },
                    success: function (res) {
                        if (res.code == 0) {
                            //  重新加载数据
                            that.$message({
                                showClose: true,
                                message: '退砼作废处理成功',
                                type: 'success'
                            });
                            // that.loadProLines();
                            that.invBakPopover = false;
                        } else {
                            that.$message({
                                showClose: true,
                                message: '退砼操作失败：' + res.msg,
                                type: 'error'
                            });
                        }
                    },
                    error: function (res) {
                        that.$message({
                            showClose: true,
                            message: '退砼操作失败，请联系管理员',
                            type: 'error'
                        });
                        return;
                    },
                    complete: function (XMLHttpRequest, status) {
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消作废操作'
                });
            })
        },
        //  显示退砼派单弹窗
        showInvBackDis(invBak) {
            let that = this;
            that.invBakDispatchForm.invBakId = invBak.id;
            that.invBakDispatchDialog.show = true;
        },
        //  任务单选择
        invBakDisIndentChange(indentId) {
            let that = this;
            that.invBakDispatchForm.indentId = indentId;
            that.loadIndentLines(indentId, function (indentLines) {
                //  生产线数据
                that.indentLines = indentLines;
                //  加载累计车次和方量
                $.ajax({
                    type: "POST",
                    url: "/pro/indent/queryTotal",
                    async: true,
                    dataType: 'json',
                    data: {
                        indentId: indentId,
                    },
                    success: function (res) {
                        if (res.code == 0) {
                            let totalData = res.data;
                            let totalCar = totalData.totalCar;
                            //  累计车次默认+1
                            totalCar = parseInt(totalCar) + 1;
                            that.invBakDispatchForm.totalCar = totalCar;
                            //  任务单的最大累计车次
                            let totalStere = totalData.totalStere;
                            that.invBakDispatchForm.totalStere = totalStere;
                            that.invBakDispatchDialog.totalStere = totalStere;
                        } else {
                            that.$message({
                                showClose: true,
                                message: '查询任务单累计数据失败：' + res.msg,
                                type: 'error'
                            });
                        }
                    },
                    error: function (res) {
                        that.$message({
                            showClose: true,
                            message: '查询任务单累计数据失败',
                            type: 'error'
                        });
                        return;
                    },
                    complete: function (XMLHttpRequest, status) {
                    }
                })
                //
            }, function () {
                // that.uploadDialog.loading = false;
            });
        },
        //  提交退砼的派单
        submitInvBackDis() {
            let that = this;
            that.invBakDispatchDialog.loading = true;
            $.ajax({
                type: "PUT",
                url: "/pro/dispatch/inv/bak",
                async: true,
                data: that.invBakDispatchForm,
                success: function (res) {
                    if (res.code == 0) {
                        //  重新加载数据
                        that.$message({
                            showClose: true,
                            message: '退砼派单处理成功',
                            type: 'success'
                        });
                        that.invBakDispatchDialog.show = false;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '退砼操作失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '退砼操作失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.invBakDispatchDialog.loading = false;
                }
            })
        },
        /**
         * 生产中断处理
         * @param disId     派单id
         *
         * */
        prodError(disId) {
            let that = this;
            if (isNull(disId)) {
                that.$message({
                    showClose: true,
                    message: '请选择派单',
                    type: 'error'
                });
                return;
            }
            that.$prompt('请输入中断原因', '操作提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputErrorMessage: '请输入中断原因',
                center: true
            }).then(({value}) => {
                if (isNull(value)) {
                    that.$message({
                        showClose: true,
                        message: '请输入中断原因',
                        type: 'error'
                    });
                    return;
                }
                $.ajax({
                    type: "PUT",
                    url: "/pro/dispatch/prod/error",
                    async: true,
                    data: {
                        disId: disId,
                        remark: value,
                    },
                    success: function (res) {
                        if (res.code == 0) {
                            //  重新加载数据
                            that.$message({
                                showClose: true,
                                message: '操作成功',
                                type: 'success'
                            });
                            that.disPopover = false;
                        } else {
                            that.$message({
                                showClose: true,
                                message: '操作失败：' + res.msg,
                                type: 'error'
                            });
                        }
                    },
                    error: function (res) {
                        that.$message({
                            showClose: true,
                            message: '操作失败，请联系管理员',
                            type: 'error'
                        });
                        return;
                    },
                    complete: function (XMLHttpRequest, status) {
                    }
                })
            }).catch(() => {

            })
        },
        refreshLab(disId) {
            let that = this;
            if (isNull(disId)) {
                that.$message({
                    showClose: true,
                    message: '请选择派单',
                    type: 'error'
                });
                return;
            }
            $.ajax({
                url: "/pro/dispatch/refresh/lab",
                async: true,
                data: {
                    disId: disId,
                },
                success: function (res) {
                    if (res.code == 0) {
                        //  重新加载数据
                        that.$message({
                            showClose: true,
                            message: '操作成功',
                            type: 'success'
                        });
                    } else {
                        that.$message({
                            showClose: true,
                            message: '操作失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '操作失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        //  上传方量适合选择的车辆
        uploadCarChange(car, fromName) {
            let that = this;
            //  设置车辆
            that[fromName].carId = car.id;
            that.chooseCar = car;
            let driverId = car.driverId;
            //  车辆最大方量
            let maxStere = car.maxStere;
            if (driverId != null) {
                that.driverList = [{
                    id: driverId,
                    name: car.driver,
                }];
                that.chooseDriver = that.driverList[0];
                that[fromName].driverId = driverId;
                that[fromName].driver = car.driver;
            } else {
                that.driverList = [];
                that.chooseDriver = null;
                that[fromName].driverId = null;
                that[fromName].driver = null;
            }
        },
        uploadDriver(driver, fromName) {
            let that = this;
            that.chooseDriver = driver;
            if (driver !== null && typeof driver === 'object') {
                that[fromName].driverId = driver.id;
                that[fromName].driver = driver.name;
            } else {
                that[fromName].driverId = -1;
                that[fromName].driver = driver;
            }
        },
        //  查询派单错误消息
        queryDisError() {
            let that = this;
            $.ajax({
                type: "GET",
                url: "/pro/dispatch/error/msg",
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        that.$message({
                            showClose: true,
                            message: res.data,
                            duration: 0,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        // 加载数据
        loadDisInvPage: function () {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/dis/page",
                async: true,
                dataType: 'json',
                success: function (res) {
                    if (res.code == 0) {
                        that.invoiceDatas = res.data;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询发货单失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        //  修改数据
        castModelChange(cast) {
            let that = this;
            //  筛选数据
            let indetList = [];
            //  全部数据
            let allIndents = that.allWorkIndents;
            if (cast == -1) {
                that.workIndents = allIndents;
                return;
            }
            for (let i = 0, s = allIndents.length; i < s; i++) {
                let ind = allIndents[i];
                if (cast == ind.castModelName) {
                    indetList.push(ind);
                }
            }
            that.workIndents = indetList;
        },
        printInvoice(invoiceId) {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/dispatch/print/" + invoiceId,
                async: true,
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        that.$message({
                            showClose: true,
                            message: '开始打印',
                            type: 'success'
                        });
                    } else {
                        that.$message({
                            showClose: true,
                            message: '打印失败:' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        // 加载近10车数据
        loadNearlyInvList: function () {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/nearly/list",
                async: true,
                dataType: 'json',
                success: function (res) {
                    if (res.code == 0) {
                        that.nearlyInvList = res.data;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询发货单失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        //  显示分车转料弹窗
        showSplitDialog(invoice) {
            let that = this;
            if (invoice == null) {
                that.$message({
                    showClose: true,
                    message: '请选择正确的发货单',
                    type: 'error'
                });
                return;
            }
            that.tranSourceInvoice = invoice;
            that.splitDialog.show = true;
        },
        //  关闭弹窗
        closeTranDialog() {
            let that = this;
            that.splitDialog.show = false;
        },
        //  查询任务单的累计方量和运输车辆
        loadAccuAndTran() {
            let that = this;
            let workIndents = that.workIndents;
            if (workIndents != null && workIndents.length > 0)
                for (let i = 0, s = workIndents.length; i < s; i++) {
                    let workIndent = workIndents[i];
                    let indentId = workIndent.id;
                    //  加载车辆数据
                    $.ajax({
                        type: "POST",
                        url: "/pro/invoice/accuAndTran",
                        async: true,
                        dataType: 'json',
                        data: {
                            indentId: indentId,
                        },
                        success: function (res) {
                            if (res.code == 0) {
                                let aat = res.data;
                                if (aat != null) {
                                    let aat = res.data;
                                    let totalCar = aat.totalCar;
                                    let sendStere = aat.sendStere;
                                    let carCodeList = aat.carCodeList;
                                    workIndent.totalCar = totalCar;
                                    workIndent.sendStere = sendStere;
                                    workIndent.carCodeList = carCodeList;
                                }
                            }
                        },
                        error: function (res) {
                            return;
                        },
                        complete: function (XMLHttpRequest, status) {
                        }
                    })
                }
        },
        //  发货方量变更
        sendStereChange() {
            let that = this;
            //  发货方量
            let sendStere = that.uploadFrom.sendStere;
            //  累计方量
            let oldStere = that.uploadDialog.totalStere;
            let newTotalStere = parseFloat(sendStere) + parseFloat(oldStere);
            that.uploadFrom.totalStere = newTotalStere;
        },
        //  退砼派单的发货单方量变更
        backSendStereChange() {
            let that = this;
            //  发货方量
            let sendStere = that.invBakDispatchForm.sendStere;
            //  累计方量
            let oldStere = that.invBakDispatchDialog.totalStere;
            let newTotalStere = parseFloat(sendStere) + parseFloat(oldStere);
            that.invBakDispatchForm.totalStere = newTotalStere;
        },
        forceComp(disId) {
            let that = this;
            if (isNull(disId)) {
                that.$message({
                    showClose: true,
                    message: '请选择派单',
                    type: 'error'
                });
                return;
            }
            that.$prompt('本操作仅适用于工控生产完成后,程序未同步,请输入强制完成原因', '强制完成', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                center: true
            }).then(({value}) => {
                $.ajax({
                    url: "/pro/dispatch/force/comp",
                    async: true,
                    type: "PUT",
                    data: {
                        disId: disId,
                        remark: value
                    },
                    success: function (res) {
                        if (res.code == 0) {
                            //  重新加载数据
                            that.$message({
                                showClose: true,
                                message: '操作成功',
                                type: 'success'
                            });
                        } else {
                            that.$message({
                                showClose: true,
                                message: '操作失败：' + res.msg,
                                type: 'error'
                            });
                        }
                    },
                    error: function (res) {
                        that.$message({
                            showClose: true,
                            message: '操作失败，请联系管理员',
                            type: 'error'
                        });
                        return;
                    },
                    complete: function (XMLHttpRequest, status) {
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消操作'
                });
            })

        },
    }
})
